package cn.scut.post.controller;

import cn.scut.post.pojo.Collect;
import cn.scut.post.pojo.Post;
import cn.scut.post.service.impl.PostCollectService;
import cn.scut.post.utils.Model;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/post/collect")
public class PostCollectController {
    PostCollectService postCollectService;

    @PutMapping("/collect")
    public Model apiCollect(@RequestBody Collect collect) {
        Post post = postCollectService.collectPost(collect.getPostid(), collect.getOpenid());
        if (post != null) {
            return new Model(true, post, "帖子具体信息如下");
        } else {
            return new Model(false, null, "帖子不存在或已经被删除或已经收藏过");
        }
    }

    @PostMapping("/users")
    public Model apiGetUserCollect(@RequestBody JSONObject jsonObject) {
        String openid = jsonObject.getString("openid");
        System.out.println(openid);
        List<Post> userCollect = postCollectService.getUserCollect(openid);
        if (userCollect != null) {
            return new Model(true, userCollect, "用户已收藏的帖子具体信息");
        } else {
            return new Model(false, null, "用户未收藏帖子或帖子链接消失");
        }
    }

    @DeleteMapping("/delete")
    public Model apiDeleteUserCollect(@RequestBody JSONObject jsonObject) {
        String postid = jsonObject.getString("postid");
        String openid = jsonObject.getString("openid");
        Post post = postCollectService.deleteUserCollect(postid, openid);
        if (post != null) {
            return new Model(true, post, "用户收藏帖子删除成功");
        } else {
            return new Model(false, null, "用户收藏帖子不存在或已经清除");
        }
    }

    @Autowired
    void setPostCollectService(PostCollectService postCollectService) {
        this.postCollectService = postCollectService;
    }
}

